package com.ccx.coachsite.bus;

import android.app.Activity;
import android.content.Intent;
import android.widget.Toast;

import com.ccx.coachsite.CoachSiteMainActivity;
import com.ccx.coachsite.MainSplash;
import com.ccx.coachsite.R;
import com.ccx.coachsite.http.BaseResp;
import com.ccx.coachsite.service.ShareMessageService;
import com.ccx.coachsite.task.LoginTask;
import com.ccx.coachsite.util.OpenShareTool;
import com.ccx.coachsite.util.SharedPreferencesUtil;

public class LoginBus extends BaseBus {
	private String mMobile;
	private String mPassword;
	private boolean isExpert;
	private boolean isShowProcess;

	public boolean isShowProcess() {
		return isShowProcess;
	}

	public void setShowProcess(boolean isShowProcess) {
		this.isShowProcess = isShowProcess;
	}

	public boolean isExpert() {
		return isExpert;
	}

	public void setxpert(boolean isExpert) {
		this.isExpert = isExpert;
	}

	public LoginBus(Activity activity) {
		super(activity, activity.getString(R.string.login_process));
		isShowProcess = true;
		isExpert = false;
		successInfo = "登录成功";
		errorInfo = "登录失败";
	}

	public void login(String mobile, String password) {
		if (isShowProcess) {
			showProgress();
		}
		mMobile = mobile;
		mPassword = password;
		LoginTask task = new LoginTask(this, mContext);
		int usertype = isExpert ? 0 : 1;
		task.setParams(mobile, password, usertype);
		task.execute();
	}

	public void onCompleted(boolean isSuccess, String desc) {
		if (isShowProcess) {
			doLoginComplete(isSuccess, desc);
		}
		if (isSuccess) {
			
			//启动分享推送检测服务
			mContext.startService(new Intent(mContext,ShareMessageService.class));
			
			//发送登录成功广播通知各View根据需要更新“分享”为“推荐”
			Intent intentLogin = new Intent( OpenShareTool.LOGIN_SUCCESS_RECEIVER );
			mContext.sendBroadcast( intentLogin );
			
			if (mContext instanceof MainSplash) {
				Intent intent = new Intent(mContext,
						CoachSiteMainActivity.class);
				mContext.startActivity(intent);
			}
			if (mActivity != null)
				mActivity.finish();
		}
	}

	private void doLoginComplete(boolean isSuccess, String desc) {
		dismissProgress();

		String toaskInfo = successInfo;
		if (!isSuccess) {
			toaskInfo = errorInfo + "," + desc;
		}
		Toast.makeText(mContext, toaskInfo, Toast.LENGTH_SHORT).show();

		if (!isSuccess && !isShowProcess) {
			// Toast.makeText(mContext, "登陆失败", Toast.LENGTH_SHORT).show();
			SharedPreferencesUtil.setValue(mContext,
					SharedKey.Shared_UserId_Key, "");
			Intent intent = new Intent(mActivity, CoachSiteMainActivity.class);
			mActivity.startActivity(intent);
			mActivity.finish();
		}
	}

	@Override
	public void callBack(int taskType, BaseResp response, String responseJson) {
		if (response.isSuccess()) {
			SharedPreferencesUtil.setValue(mContext,
					SharedKey.Shared_Mobile_Key, mMobile);
			SharedPreferencesUtil.setValue(mContext,
					SharedKey.Shared_Password_Key, mPassword);
			SharedPreferencesUtil.setValue(mContext,
					SharedKey.Shared_Login_Type, "-1");
			LoadDataBus loadData = new LoadDataBus(mContext, this);
			loadData.loadData(mMobile);

		} else {
			doLoginComplete(false, response.getDesc());
		}
	}
}